#include "gdn/SceneManager.h"

namespace gdn
{
	SceneManager::SceneManager()
	{
		active.second = NULL;
	}

	SceneManager::~SceneManager()
	{
		for ( std::map< std::string, Scene* >::iterator it = scenes.begin(); it != scenes.end(); ++it )
		{
			RemoveScene( it->first );
		}
	}

	void SceneManager::AddScene( const std::string& name, Scene* scene )
	{
		scenes.insert( std::pair< std::string, Scene* >( name, scene ) );
	}

	void SceneManager::RemoveScene( const std::string& name )
	{
		if ( !( GetActiveSceneName() == name ) )
		{
			scenes.erase( name );
		}
	}

	bool SceneManager::DoesSceneExist( const std::string& name ) const
	{
		if ( FindScene( name ) == NULL )
		{
			return false;
		}
		return true;
	}

	std::vector< std::string > SceneManager::GetSceneList() const
	{
		std::vector< std::string > sceneList;

		for ( std::map< std::string, Scene* >::const_iterator it = scenes.begin(); it != scenes.end(); ++it )
		{
			sceneList.push_back( it->first );
		}

		return sceneList;
	}

	Scene* SceneManager::FindScene( const std::string& name ) const
	{
		std::map< std::string, Scene* >::const_iterator found = FindSceneIterator( name );

		if ( found != scenes.end() )
		{
			return found->second;
		}
		return NULL;
	}

	void SceneManager::SetActiveScene( const std::string& name )
	{
		if ( active.second != NULL )
		{
			active.second->Terminate();
		}

		active.first  = name;
		active.second = FindScene( name );

		if ( active.second != NULL ) // Here
		{
			active.second->Initialize();
		}
	}

	const std::string& SceneManager::GetActiveSceneName() const
	{
		return active.first;
	}

	std::map< std::string, Scene* >::const_iterator SceneManager::FindSceneIterator( const std::string& name ) const
	{
		return scenes.find( name );
	}
}
